<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function ajax(url) {
            return new Promise((resolve, reject) => {
                let xhr = new XMLHttpRequest()
                xhr.open("get", url, true)
                xhr.send()
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4) {
                        if (xhr.status >= 200 && xhr.status < 300) {
                            resolve(xhr.responseText)
                        } else {
                            reject(xhr.responseText)
                        }
                    }
                }
            })
        }
        function* gen() {
            let res = yield ajax("1.json")
            console.log("第一个请求的结果", res)
            let res2 = yield ajax("2.json")
            console.log("第二个请求的结果", res2)
        }
        //自动版本，递归实现
        function AutoRun(gen) {
            let g = gen();
            function next(data) {
                let res = g.next(data);
                if (res.done) return
                res.value.then(function (data) {
                    next(data);
                });
            }
            next();
        }
        AutoRun(gen)
        //手动版本
        //let g = gen()
        //console.log(g.next()) //g.next
        // g.next().value.then(data => {
        //     g.next(data).value.then(res => {
        //         g.next(res)
        //     })
        // })
        //async await 属于内置的自动执行器！


    </script>
</body>

</html>